﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using Newtonsoft.Json;
using UnityEditor;

public class StateMachine : MonoBehaviour
{
	FSMEnemy fsm;
	int unique_id = 1;

	public bool IsStart { get; protected set; } = false;
	private void Start()
	{
		RoleCtrl role = GetComponent<RoleCtrl>();
		EnemyFSMStateChase chaseState = new EnemyFSMStateChase(role);
		EnemyFSMStateEscape escapeState = new EnemyFSMStateEscape(role);
		EnemyFSMStatePatrol patrolState = new EnemyFSMStatePatrol(role);


		EnemySTChase2Escape chase2Escape = new EnemySTChase2Escape("Chase2Escape", chaseState, escapeState, 5 , role);
		EnemySTChase2Patrol chase2Patrol = new EnemySTChase2Patrol("Chase2Patrol", chaseState, patrolState, 0, role);

		EnemySTEscape2Chase escape2Chase = new EnemySTEscape2Chase("Escape2Chase", escapeState, chaseState, 10, role);
		EnemySTEscape2Patrol escape2Patrol = new EnemySTEscape2Patrol("Escape2Patrol", escapeState, patrolState, 0, role);

		EnemySTPatrol2Chase patrol2Chase = new EnemySTPatrol2Chase("Patrol2Chase", patrolState, chaseState, 10, role);
		EnemySTPatrol2Escape patrol2Escape = new EnemySTPatrol2Escape("Patrol2Escape", patrolState, escapeState, 5, role);

		chaseState.AddTransition(chase2Escape);
		chaseState.AddTransition(chase2Patrol);

		escapeState.AddTransition(escape2Chase);
		escapeState.AddTransition(escape2Patrol);

		patrolState.AddTransition(patrol2Chase);
		patrolState.AddTransition(patrol2Escape);

		fsm = new FSMEnemy($"Enemy{unique_id}", patrolState, role);

		fsm.AddState(escapeState);
		fsm.AddState(chaseState);

		chaseState.SetMachine(fsm);
		escapeState.SetMachine(fsm);
		patrolState.SetMachine(fsm);

		fsm.BeginMachine();
	}
	private void FixedUpdate()
	{
		if(IsStart)
			fsm.UpdateMachine(Time.fixedDeltaTime);
	}
	
	public void StartMachine()
	{
		IsStart = true;
	}
#if UNITY_EDITOR


	[ContextMenu("OutPutJson")]
	public void CreatJson()
	{
		List<TransitionLevel> levels = new List<TransitionLevel>()
		{
			new TransitionLevel()
			{
				 Levle = 0,
				 Params = new Dictionary<TransitionParamType, TransitionParam>()
				 {
					 {
							 TransitionParamType.Prtrol,
							 new TransitionParam(){ VigilancePatrol = 0, VigilanceEscape = 180, VigilanceChase = 270 , ChaseMaxTime = 0, Priority = 0}
					 },
					 {
							 TransitionParamType.Escape,
							 new TransitionParam(){ VigilancePatrol = 320, VigilanceEscape = 200, VigilanceChase = 300 , ChaseMaxTime = 0, Priority = 5}
					 },
					{
							 TransitionParamType.Chase,
							 new TransitionParam(){ VigilancePatrol = 240, VigilanceEscape = 120, VigilanceChase = 180 , ChaseMaxTime = 5, Priority = 10}
					 },
				 }
			},

			new TransitionLevel()
			{
				 Levle = 1,
				 Params = new Dictionary<TransitionParamType, TransitionParam>()
				 {
					 {
							 TransitionParamType.Prtrol,
							 new TransitionParam(){ VigilancePatrol = 0, VigilanceEscape = 250, VigilanceChase = 330 , ChaseMaxTime = 0, Priority = 0}
					 },
					 {
							 TransitionParamType.Escape,
							 new TransitionParam(){ VigilancePatrol = 400, VigilanceEscape = 280, VigilanceChase = 380 , ChaseMaxTime = 0, Priority = 5}
					 },
					{
							 TransitionParamType.Chase,
							 new TransitionParam(){ VigilancePatrol = 330, VigilanceEscape = 180, VigilanceChase = 270 , ChaseMaxTime = 6, Priority = 10}
					 },
				 }
			},

			new TransitionLevel()
			{
				 Levle = 2,
				 Params = new Dictionary<TransitionParamType, TransitionParam>()
				 {
					 {
							 TransitionParamType.Prtrol,
							 new TransitionParam(){ VigilancePatrol = 0, VigilanceEscape = 320, VigilanceChase = 390 , ChaseMaxTime = 0, Priority = 0}
					 },
					 {
							 TransitionParamType.Escape,
							 new TransitionParam(){ VigilancePatrol = 480, VigilanceEscape = 360, VigilanceChase = 460 , ChaseMaxTime = 0, Priority = 5}
					 },
					{
							 TransitionParamType.Chase,
							 new TransitionParam(){ VigilancePatrol = 420, VigilanceEscape = 240, VigilanceChase = 360 , ChaseMaxTime = 7, Priority = 10}
					 },
				 }
			},

			new TransitionLevel()
			{
				 Levle = 3,
				 Params = new Dictionary<TransitionParamType, TransitionParam>()
				 {
					 {
							 TransitionParamType.Prtrol,
							 new TransitionParam(){ VigilancePatrol = 0, VigilanceEscape = 390, VigilanceChase = 450 , ChaseMaxTime = 0, Priority = 0}
					 },
					 {
							 TransitionParamType.Escape,
							 new TransitionParam(){ VigilancePatrol = 560, VigilanceEscape = 440, VigilanceChase = 540 , ChaseMaxTime = 0, Priority = 5}
					 },
					{
							 TransitionParamType.Chase,
							 new TransitionParam(){ VigilancePatrol = 510, VigilanceEscape = 300, VigilanceChase = 450 , ChaseMaxTime = 8, Priority = 10}
					 },
				 }
			},

		};
		Debug.Log(JsonConvert.SerializeObject(levels));
	}
	public Vector3 center = Vector3.zero;
	public Vector3 size = Vector3.zero;
	private void OnDrawGizmos()
	{
		Gizmos.color = Color.red;
		Gizmos.DrawWireCube(center, size);
	}
#endif
}
